package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;

import view.MainWindow;
import model.BinForm;
import model.Carnet;
import model.Contact;
/**
 * this class is to set the action listener of the button save in a formulary
 * @author Estelle
 *
 */
public class SaveFormulaire implements ActionListener {
//SaveFormulaire(LastName,FirstName,NumberPerso,homeAddress,companyAddress,homePage,email));
	private BinForm bin;
	private JFrame	saveWindow = new JFrame();
	private JFrame	mainWindow = new JFrame();
	
	
	public SaveFormulaire(BinForm b, JFrame	saveWindow ,JFrame	mainWindow){
		this.bin = b;
		this.saveWindow = saveWindow;
		this.mainWindow = mainWindow;
	}

	 @Override
     public void actionPerformed(ActionEvent event) {
		 
		 String last =bin.getLastName().getText();
		 String first = bin.getFirstName().getText();
		 String num = bin.getNumberPerso().getText();
		 String homeAddress = bin.getHomeAddress().getText();
		 String companyAddress = bin.getCompanyAddress().getText();
		 String homePage = bin.getHomePage().getText();
		 String	mail = bin.getEmail().getText();
		 String	groupe = bin.getGroupe().getText();
		 String name = last + " " + first;
		   
    		 Carnet carnet = new Carnet("");
    		 FileDoc file = new FileDoc();
    		 carnet.copy(file.recupCarnet("Estelle"));
    		 
    		 Contact newContact = new Contact(first,last,num);
    		 newContact.setCompanyAddress(companyAddress);
    		 newContact.setEmail(mail);
    		 newContact.setHomePage(homePage);
    		 newContact.setHomeAddress(homeAddress);
    		 newContact.setGroupe(groupe);
    		 
    		 int Id = carnet.getIDContact(carnet.searchContact(name)); 
    		 if(!carnet.searchContact(name).emptyContact() && Id>0){
    			 carnet.getContent().remove(Id);
    		 }
    		 
    		 carnet.addNewContact(newContact);
    		 file.orderCarnet(carnet);
    		 mainWindow.dispose();
    		 saveWindow.dispose();
    		 new MainWindow();
    	
     }
}
